﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using MasMembers.DAL;

namespace MasMembers
{
    public partial class MainForm : Form
    {
        private const String MESSAGE_ERROR_IN_SAVE = "error";
        private const String MESSAGE_SAVED_SUCCESSFULLY = "successs";
        private const String MESSAGE_MEM_NOT_SELECTED = "notExist";
        private const String MESSAGE_DATA_NOT_COMPLETED = "data not completed";
        private const String MESSAGE_DELETE_SUCCESSFULLY = "done";
        private const String MESSAGE_NUM_ERROR = "error in NUM";

        private static Int32 flag = 0;
        private String picsPath = "";
        private String picExt = "";

        public Int32 currentMem = -1;
        public Int32 currentFamilyMem = -1;
        public Int32 currentAddEdu = -1;
        public Int32 currentCourse = -1;
        public UserEntity currentUser;
        public LoginForm loginForm;




        public MainForm()
        {
            InitializeComponent();
        }

        /**
         * get & set Entiites to and from the forms
         * 
         * **/
        public void publishAllPanelsData(MemberBasicInfoEntity entity)
        {
            clearAllPagesData();
            publishMemBasicInfoFormFromEntity(entity);
            publishPartnerDataToForm(Partner.GetPartnerByMemId(currentMem));
            publishJobEntityToForm(MJob.GetJobByMemId(currentMem));
            refreshFamilyGrid();
            refreshEduGrid();
            refreshCoursesGrid();
        }

        private MemberBasicInfoEntity fillBasicInfoEntityFromForm()
        {
            if (txtMemName.Text == "" || txtGroup.Text == "")
                return null;
            else
            {
                MemberBasicInfoEntity entity = new MemberBasicInfoEntity();
                entity.m_Address_City = txtAddressCity.Text;
                entity.m_Address_Place = cmboRegions.Text;
                entity.m_Address_Street = txtAddressStreet.Text;
                entity.m_Blood_Type = cmboBloodType.Text;
                entity.m_DOB = pkrDOB.Value.Date;
                entity.m_Email = txtEmail.Text;
                entity.m_FaceBook = txtFacebook.Text;
                entity.m_Gender = cmboGender.Text;
                entity.m_Group_Id = GroupsNames.GetGroupByG_Name(txtGroup.Text).G_Id;
                entity.m_Id_Issue_Date = pkrIdIssueDate.Value.Date;
                entity.m_Id_Issue_Palce = txtIdIssuePlace.Text;
                entity.m_Id_Num = txtIdNum.Text;
                entity.m_Id_Type = cmboIdType.Text;
                entity.m_Mem_Name = txtMemName.Text;
                entity.m_Mobile1 = txtMobile1.Text;
                entity.m_Mobile2 = txtMobile2.Text;
                entity.m_Phone = txtPhone.Text;
                entity.m_POB = txtPOB.Text;
                entity.m_Status = cmboStatus.Text;
                entity.m_Study = txtStudy.Text;
                entity.m_Study_Grade = txtStudyGrade.Text;
                entity.m_Study_Year = txtStudyYear.Text;
                entity.m_Study_Group = cmboStudyGroup.Text;
                entity.m_Twitter = txtTwitter.Text;
                entity.m_military_status = m_military_status.Text;

                //Add photo path
                entity.Photo = "";
                if (this.openFileDialog1.FileName != "openFileDialog1")
                {
                    entity.Photo = this.txtMemName.Text.ToString() + "." + picExt;
                    if (File.Exists(picsPath + "\\" + this.txtMemName.Text.ToString() + "." + picExt))
                    {
                        File.Delete(picsPath + "\\" + this.txtMemName.Text.ToString() + "." + picExt);
                    }
                    File.Copy(this.openFileDialog1.FileName, picsPath + this.txtMemName.Text.ToString() + "." + picExt);
                }
                if (currentMem != -1)
                    entity.Photo = this.txtMemName.Text.ToString() + "." + picExt;

                return entity;
            }
        }

        private void publishMemBasicInfoFormFromEntity(MemberBasicInfoEntity entity)
        {
            currentMem = entity.m_Id;
            txtAddressCity.Text = entity.m_Address_City;
            cmboRegions.Text = entity.m_Address_Place;
            txtAddressStreet.Text = entity.m_Address_Street;
            cmboBloodType.Text = entity.m_Blood_Type;
            pkrDOB.Value = entity.m_DOB;
            txtEmail.Text = entity.m_Email;
            txtFacebook.Text = entity.m_FaceBook;
            cmboGender.Text = entity.m_Gender;
            txtGroup.Text = GroupsNames.GetGroupByG_ID(entity.m_Group_Id).G_Name;
            pkrIdIssueDate.Value = entity.m_Id_Issue_Date;
            txtIdIssuePlace.Text = entity.m_Id_Issue_Palce;
            txtIdNum.Text = entity.m_Id_Num;
            cmboIdType.Text = entity.m_Id_Type;
            txtMemName.Text = entity.m_Mem_Name;
            txtMobile1.Text = entity.m_Mobile1;
            txtMobile2.Text = entity.m_Mobile2;
            txtPhone.Text = entity.m_Phone;
            txtPOB.Text = entity.m_POB;
            cmboStatus.Text = entity.m_Status;
            txtStudy.Text = entity.m_Study;
            txtStudyGrade.Text = entity.m_Study_Grade;
            txtStudyYear.Text = entity.m_Study_Year;
            cmboStudyGroup.Text = entity.m_Study_Group;
            txtTwitter.Text = entity.m_Twitter;
            m_military_status.Text = entity.m_military_status;
            if (entity.Photo != "")
            {
                picExt = entity.Photo.Substring(entity.Photo.IndexOf('.') + 1);
                try
                {
                    if (!Directory.Exists(picsPath + "\\temp"))
                    {
                        Directory.CreateDirectory(picsPath + "\\temp");
                    }
                    if (!File.Exists(picsPath + "\\temp" + "\\" + entity.Photo))
                    {
                        File.Copy(picsPath + entity.Photo, picsPath + "\\temp" + "\\" + entity.Photo);
                    }

                    flag = 1;
                    // Connect the Paint event of the PictureBox to the event handler method.
                    this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
                    this.pictureBox1.Image = Image.FromFile(picsPath + "\\temp" + "\\" + entity.Photo);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("صورة هذا العضو غير موجودة");
                    //TODO add default image. 
                }
            }
        }

        private FamilyEntity fillFamilyEntityFromForm()
        {
            if (txtFamilyName.Text == "" || cmboFamlilyRelation.Text == "")
            {
                return null;
            }
            else
            {
                FamilyEntity entity = new FamilyEntity();
                entity.DOB = txtFamilyDOB.Value.Date;
                entity.Educ = txtFamilyEducation.Text;
                entity.Existing = Int32.Parse(lblExitingMember.Text);
                entity.F_Job = txtFamilyJob.Text;
                entity.F_Name = txtFamilyName.Text;
                entity.F_Type = cmboFamlilyRelation.Text;
                entity.Mem_Id = currentMem;
                entity.School = txtFamilySchool.Text;
                entity.Still_Study = chkStillStudy.Checked;
                entity.Study_Year = txtFamilyStudyYear.Text;
                entity.Id = currentFamilyMem;
                return entity;
            }
        }

        public void publishFamilyDataToForm(FamilyEntity entity)
        {
            txtFamilyDOB.Value = entity.DOB;
            txtFamilyEducation.Text = entity.Educ;
            lblExitingMember.Text = entity.Existing.ToString();
            txtFamilyJob.Text = entity.F_Job;
            txtFamilyName.Text = entity.F_Name;
            cmboFamlilyRelation.Text = entity.F_Type;
            currentMem = entity.Mem_Id;
            txtFamilySchool.Text = entity.School;
            chkStillStudy.Checked = entity.Still_Study;
            chkExisting.Checked = entity.Existing == -1 ? false : true;
            txtFamilyStudyYear.Text = entity.Study_Year;
            this.currentFamilyMem = entity.Id;
        }

        private PartnerEntity fillPartnerEntityFromForm()
        {
            PartnerEntity entity = new PartnerEntity();
            if (txtPartnerName.Text != "")
            {
                entity.POB = txtPartnerPOB.Text;
                entity.P_Name = txtPartnerName.Text;
                entity.Job_Tittle = txtPartnerJobTittle.Text;
                entity.Job_Place = txtPartnerJobPlace.Text;
                entity.P_Job = txtPartnerJob.Text;
                entity.Income = txtPartnerIncome.Text == "" ? 0 : Int32.Parse(txtPartnerIncome.Text);
                entity.Id_Num = txtPartnerIdNum.Text;
                entity.Education = txtPartnerEduc.Text;
                entity.DOB = pkrPartnerDOB.Value.Date;
                entity.TypeOfExist = cmboPartnerTypeOfExists.Text;
                entity.Mem_Id = currentMem;
                entity.Existing = lblExistingPartner.Text == "" ? -1 : Int32.Parse(lblExistingPartner.Text);
                return entity;
            }
            else
            {
                return null;
            }
        }

        public void publishPartnerDataToForm(PartnerEntity entity)
        {
            if (entity != null)
            {
                txtPartnerPOB.Text = entity.POB;
                txtPartnerName.Text = entity.P_Name;
                txtPartnerJobTittle.Text = entity.Job_Tittle;
                txtPartnerJobPlace.Text = entity.Job_Place;
                txtPartnerJob.Text = entity.P_Job;
                txtPartnerIncome.Text = entity.Income.ToString();
                txtPartnerIdNum.Text = entity.Id_Num;
                txtPartnerEduc.Text = entity.Education;
                pkrPartnerDOB.Value = entity.DOB;
                lblExistingPartner.Text = entity.Existing.ToString();
                cmboPartnerTypeOfExists.Text = entity.TypeOfExist;
                chkPartnerExisting.Checked = entity.Existing == -1 ? false : true;
            }
        }

        private EducationEntity fillEducationEntityFromForm()
        {
            if (cmboAddEduStatus.Text != "" && cmboAddEduLevel.Text != "" && cmboAddEduType.Text != "")
            {
                EducationEntity entity = new EducationEntity();
                entity.Edu_Geha = txtAddEduGeha.Text;
                entity.Edu_Grade = txtAddEduDegree.Text;
                entity.Edu_Level = cmboAddEduLevel.Text;
                entity.Edu_Status = cmboAddEduStatus.Text;
                entity.Edu_Takhsos = txtAddEduTakhosos.Text;
                entity.Edu_Type = cmboAddEduType.Text;
                entity.Mem_Id = currentMem;
                entity.Id = currentAddEdu;
                return entity;
            }
            else
            {
                return null;
            }
        }

        private void publishEducationDataToForm(EducationEntity entity)
        {
            txtAddEduGeha.Text = entity.Edu_Geha;
            txtAddEduDegree.Text = entity.Edu_Grade;
            cmboAddEduLevel.Text = entity.Edu_Level;
            cmboAddEduStatus.Text = entity.Edu_Status;
            txtAddEduTakhosos.Text = entity.Edu_Takhsos;
            cmboAddEduType.Text = entity.Edu_Type;
            currentMem = entity.Mem_Id;
            currentAddEdu = entity.Id;
        }

        private CoursesEntity filCourseEntityFromForm()
        {
            if (cmboCourseType.Text != "" && txtCourseName.Text != "")
            {
                CoursesEntity entity = new CoursesEntity();
                entity.Course_From = txtCoursePlace.Text;
                entity.Course_Grade = txtCourseGrade.Text;
                entity.Course_Name = txtCourseName.Text;
                entity.Course_Type = cmboCourseType.Text;
                entity.Course_Year = txtCourseYear.Text;
                entity.Mem_Id = currentMem;
                entity.C_Id = currentCourse;
                return entity;
            }
            else
            {
                return null;
            }
        }

        private void publishCourseEntityToForm(CoursesEntity entity)
        {
            txtCoursePlace.Text = entity.Course_From;
            txtCourseGrade.Text = entity.Course_Grade;
            txtCourseName.Text = entity.Course_Name;
            cmboCourseType.Text = entity.Course_Type;
            txtCourseYear.Text = entity.Course_Year;
            currentCourse = entity.C_Id;
        }

        private JobEntity fillJobEntityFromForm()
        {
            try
            {
                if (cmboMainJobType.Text == "")
                {
                    return null;
                }
                else
                {
                    JobEntity entity = new JobEntity();
                    entity.Addit_Salary = Decimal.ToInt32(txtAddSalary.Value);
                    entity.Income = Decimal.ToInt32(txtIncome.Value);
                    entity.Main_Job = txtMainJob.Text;
                    entity.Main_Salary = Decimal.ToInt32(txtMainSalary.Value);
                    entity.Mem_Num = currentMem;
                    entity.MJob_Geha = txtMainJobGeha.Text;
                    entity.MJob_Level = txtMainJobLevel.Text;
                    entity.MJob_Name = txtMainJobName.Text;
                    entity.MJob_Type = cmboMainJobType.Text;
                    entity.Sec_Job = txtSecJob.Text;
                    entity.SJob_Geha = txtSecJobGeha.Text;
                    entity.Daily_Working_Hours = Int32.Parse(txtDailyWorkHours.Text);
                    entity.Monthly_Working_Hours = Int32.Parse(txtMonthlyWorkHours.Text);
                    entity.Working_Hours_From = Int32.Parse(txtWorkingHoursFrom.Text);
                    entity.Working_Hours_To = Int32.Parse(txtWorkingHoursTo.Text);
                    entity.Working_Days = getWorkingDaysFromForm();
                    entity.IsGovern_Job = chxIsGovern.Checked;
                    return entity;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private void publishJobEntityToForm(JobEntity entity)
        {
            if (entity != null)
            {
                txtAddSalary.Value = entity.Addit_Salary;
                txtIncome.Value = entity.Income;
                txtMainJob.Text = entity.Main_Job;
                txtMainSalary.Value = entity.Main_Salary;
                txtMainJobGeha.Text = entity.MJob_Geha;
                txtMainJobLevel.Text = entity.MJob_Level;
                txtMainJobName.Text = entity.MJob_Name;
                cmboMainJobType.Text = entity.MJob_Type;
                txtSecJob.Text = entity.Sec_Job;
                txtSecJobGeha.Text = entity.SJob_Geha;
                txtDailyWorkHours.Text = entity.Daily_Working_Hours.ToString();
                txtMonthlyWorkHours.Text = entity.Monthly_Working_Hours.ToString();
                txtWorkingHoursFrom.Text = entity.Working_Hours_From.ToString();
                txtWorkingHoursTo.Text = entity.Working_Hours_To.ToString();
                chxIsGovern.Checked = entity.IsGovern_Job;
                publishWorkingDaysToForm(entity.Working_Days);
            }
        }
        //private 
        /***
         * End Of get & set Entiites to and from the forms
         * 
         * **/



        /**
         *  Clear Data Methods
         ***/
        private void clearBasicInfoPageData()
        {
            currentMem = -1;
            currentFamilyMem = -1;

            txtAddressCity.Text = "";
            cmboRegions.Text = "";
            txtAddressStreet.Text = "";
            txtEmail.Text = "";
            txtFacebook.Text = "";
            //txtGroup.Text = "";
            txtIdIssuePlace.Text = "";
            txtIdNum.Text = "";
            txtMemName.Text = "";
            txtMobile1.Text = "";
            txtMobile2.Text = "";
            txtPhone.Text = "";
            txtPOB.Text = "";
            txtStudy.Text = "";
            txtStudyGrade.Text = "";
            cmboStudyGroup.Text = "";
            txtStudyYear.Text = "";
            txtTwitter.Text = "";
            pkrDOB.Value = DateTime.Now.Date;
            pkrIdIssueDate.Value = DateTime.Now.Date;
            cmboBloodType.Text = "";
            cmboGender.Text = "";
            cmboIdType.Text = "";
            cmboStatus.Text = "";
            m_military_status.Text = "";
            this.openFileDialog1.FileName = "openFileDialog1";
            pictureBox1.Image = null;
            picExt = "";
        }

        private void clearFamilyData()
        {
            txtFamilyDOB.Value = DateTime.Now.Date;
            txtFamilyEducation.Text = "";
            lblExitingMember.Text = "";
            txtFamilyJob.Text = "";
            txtFamilyName.Text = "";
            cmboFamlilyRelation.Text = "";
            txtFamilySchool.Text = "";
            chkStillStudy.Checked = false;
            chkExisting.Checked = false;
            txtFamilyStudyYear.Text = "";
            lblExitingMember.Text = "-1";
            currentFamilyMem = -1;
        }

        private void clearPartnerData()
        {
            txtPartnerEduc.Text = "";
            txtPartnerIdNum.Text = "";
            txtPartnerIncome.Text = "";
            txtPartnerJob.Text = "";
            txtPartnerJobPlace.Text = "";
            txtPartnerJobTittle.Text = "";
            txtPartnerName.Text = "";
            txtPartnerPOB.Text = "";
            pkrPartnerDOB.Value = DateTime.Now;
            cmboPartnerTypeOfExists.Text = "";
            chkPartnerExisting.Checked = false;
            lblExistingPartner.Text = "-1";
        }

        private void clearAddEduPageData()
        {
            txtAddEduDegree.Text = "";
            txtAddEduGeha.Text = "";
            txtAddEduTakhosos.Text = "";
            cmboAddEduLevel.Text = "";
            cmboAddEduStatus.Text = "";
            cmboAddEduType.Text = "";
            currentAddEdu = -1;
        }

        private void clearCoursesPageData()
        {
            cmboCourseType.Text = "";
            txtCourseGrade.Text = "";
            txtCourseName.Text = "";
            txtCoursePlace.Text = "";
            txtCourseYear.Text = "";
            currentCourse = -1;

        }

        private void clearJobPageData()
        {
            txtAddSalary.Value = 0;
            txtIncome.Value = 0;
            txtMainJob.Text = "";
            txtMainSalary.Value = 0;
            txtMainJobGeha.Text = "";
            txtMainJobLevel.Text = "";
            txtMainJobName.Text = "";
            cmboMainJobType.Text = "";
            txtSecJob.Text = "";
            txtSecJobGeha.Text = "";
            txtDailyWorkHours.Text = "";
            txtMonthlyWorkHours.Text = "";
            txtWorkingHoursFrom.Text = "";
            txtWorkingHoursTo.Text = "";
            resetWorkingDays();
        }

        private void clearAllPagesData()
        {
            this.currentFamilyMem = -1;
            this.currentMem = -1;
            gridFamily.DataSource = new List<FamilyEntity>();
            gridCourses.DataSource = new List<CoursesEntity>();
            gridAddEdu.DataSource = new List<EducationEntity>();

            clearBasicInfoPageData();
            clearFamilyData();
            clearPartnerData();
            clearAddEduPageData();
            clearCoursesPageData();
            clearJobPageData();
        }

        private void refreshCombos()
        {
            //cmboRegions.Items.Clear();
            //cmboRegions.Items.AddRange(MemberBasicInfo.getRegions().ToArray());

            //m_military_status.Items.Clear();
            //m_military_status.Items.AddRange(MemberBasicInfo.GetMilitaryStatusValues().ToArray());
        }
        /**
         *  End Of Clear Data Methods
         * *****/

        private String getWorkingDaysFromForm()
        {
            String workingDays = "";
            if (chkWorkSat.Checked)
                workingDays += "1";
            if (chkWorkSun.Checked)
                workingDays += "2";
            if (chkWorkMon.Checked)
                workingDays += "3";
            if (chkWorkTues.Checked)
                workingDays += "4";
            if (chkWorkWed.Checked)
                workingDays += "5";
            if (chkWorkThu.Checked)
                workingDays += "6";
            if (chkWorkFri.Checked)
                workingDays += "7";

            return workingDays;
        }

        private void publishWorkingDaysToForm(String workingDays)
        {
            if (workingDays.Contains("1"))
                chkWorkSat.Checked = true;
            if (workingDays.Contains("2"))
                chkWorkSun.Checked = true;
            if (workingDays.Contains("3"))
                chkWorkMon.Checked = true;
            if (workingDays.Contains("4"))
                chkWorkTues.Checked = true;
            if (workingDays.Contains("5"))
                chkWorkWed.Checked = true;
            if (workingDays.Contains("6"))
                chkWorkThu.Checked = true;
            if (workingDays.Contains("7"))
                chkWorkFri.Checked = true;
        }

        private void resetWorkingDays()
        {
            chkWorkSat.Checked = false;
            chkWorkSun.Checked = false;
            chkWorkMon.Checked = false;
            chkWorkTues.Checked = false;
            chkWorkWed.Checked = false;
            chkWorkThu.Checked = false;
            chkWorkFri.Checked = false;
        }

        private void showMessage(String messageType)
        {
            String message = "";
            MessageBoxIcon icons = MessageBoxIcon.Error;
            switch (messageType)
            {
                case MESSAGE_MEM_NOT_SELECTED: message = "يجب إختيار بيان أولاً"; break;
                case MESSAGE_ERROR_IN_SAVE: message = "خطأ أثناء الحفظ"; break;
                case MESSAGE_DATA_NOT_COMPLETED: message = "يجب إدخال كل البيانات المطلوبة"; break;
                case MESSAGE_DELETE_SUCCESSFULLY: message = "تم الحذف بنجاح"; icons = MessageBoxIcon.Information; break;
                case MESSAGE_SAVED_SUCCESSFULLY: message = "تم الحفظ بنجاح"; icons = MessageBoxIcon.Information; break;
                case MESSAGE_NUM_ERROR: message = "يجب ادخال رقم البطاقة"; icons = MessageBoxIcon.Information; break;
            }
            MessageBox.Show(message, "رسالة", MessageBoxButtons.OK, icons);
        }

        private bool showConfirmDeleteMessage()
        {
            DialogResult result = MessageBox.Show("هل تريد حذف هذا البيان ؟", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult.Yes == result)
                return true;
            else
                return false;
        }

        private void refreshFamilyGrid()
        {
            List<FamilyEntity> list = Family.GetFamilyDataByMemId(currentMem);
            gridFamily.DataSource = list;
        }

        private void refreshEduGrid()
        {
            List<EducationEntity> list = Education.GetEducationDataByMemId(currentMem);
            gridAddEdu.DataSource = list;
        }

        private void refreshCoursesGrid()
        {
            List<CoursesEntity> list = Courses.GetCourseByMemID(currentMem);
            gridCourses.DataSource = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            saveBasicInfo();
        }

        private void saveBasicInfo()
        {
            MemberBasicInfoEntity entity = fillBasicInfoEntityFromForm();
            if (entity.m_Id_Num != "")
            {
                if (entity != null)
                {
                    bool flag = true;
                    if (currentMem == -1)
                    {
                        currentMem = MemberBasicInfo.Add(entity);
                        if (currentMem == -1)
                            flag = false;
                    }
                    else
                    {
                        entity.m_Id = currentMem;
                        flag &= MemberBasicInfo.Update(entity);
                    }

                    if (flag)
                    {
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                        clearBasicInfoPageData();
                    }
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
                else
                {
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                }
            }
            else
                showMessage(MESSAGE_NUM_ERROR);
        }


        private void txtMemName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                List<MemberBasicInfoEntity> list;
                if (currentUser.UType == User.ADMIN_USER_UType)
                {
                    list = MemberBasicInfo.GetMembersByName("%" + ((TextBox)sender).Text + "%");
                }
                else
                {
                    list = MemberBasicInfo.GetMembersByNameFilteringByName("%" + ((TextBox)sender).Text + "%", currentUser.Group);
                }
                if (list.Count == 0)
                    MessageBox.Show("هذا العضو غير موجود");
                else if (list.Count == 1)
                    publishAllPanelsData(list[0]);
                else
                {
                    //gridMembers.DataSource = list;
                    //gridMembers.Visible = true;
                    SelectMemberDialoge dialoge = new SelectMemberDialoge(0);
                    dialoge.mainForm = this;
                    dialoge.ShowDialog(this);
                }

            }
        }


        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            /*              this.rprtPayedduringPeriod1.SetDataSource(MembersDataSet1);
                            personPaymentQueryTableAdapter1.Fill(MembersDataSet1.PersonPaymentQuery);
                            this.rprtPayedduringPeriod1.SetParameterValue("startDate", pkrstartDate.Value);
                            this.rprtPayedduringPeriod1.SetParameterValue("endDate", pkrEndDate.Value);
                            rprtViewer.ReportSource = this.rprtPayedduringPeriod1;
             **/
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (currentMem != -1)
            {
                if (showConfirmDeleteMessage())
                {
                    if (MemberBasicInfo.Delete(currentMem))
                    {
                        clearAllPagesData();
                        showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                    }
                }

            }
            else
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            clearAllPagesData();
            refreshCombos();
        }

        private void txtIdNum_Click(object sender, EventArgs e)
        {
            MessageBox.Show("عدد الأرقام:" + txtIdNum.Text.Length);
        }


        private void button5_Click_1(object sender, EventArgs e)
        {
            if (currentUser.UType == User.ADMIN_USER_UType)
            {
                SelectGroupDialoge dialoge = new SelectGroupDialoge();
                dialoge.mainForm = this;
                dialoge.ShowDialog(this);
            }
            else
            {
                txtGroup.Text = GroupsNames.GetGroupByG_ID(currentUser.Group).G_Name;
            }
        }

        private void FilltxtGroup()
        {
            if (currentUser.UType == User.ADMIN_USER_UType)
            {
                SelectGroupDialoge dialoge = new SelectGroupDialoge();
                dialoge.mainForm = this;
                dialoge.ShowDialog(this);
            }
            else
            {
                txtGroup.Text = GroupsNames.GetGroupByG_ID(currentUser.Group).G_Name;
            }
        }

        private void chkExisting_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExisting.Checked)
            {
                SelectMemberDialoge dialoge = new SelectMemberDialoge(2);
                dialoge.mainForm = this;
                dialoge.ShowDialog(this);
            }
            else
            {
                lblExistingPartner.Text = "";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            clearFamilyData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
            else
            {
                FamilyEntity entity = fillFamilyEntityFromForm();
                bool flag = true;
                if (entity == null)
                {
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                }
                else
                {
                    if (entity.Id == -1)
                        flag &= Family.Add(entity) == -1 ? false : true;
                    else
                        flag &= Family.Update(entity);

                    if (flag)
                    {
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                        clearFamilyData();
                        refreshFamilyGrid();
                    }
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }
        }

        private void gridFamily_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 || e.ColumnIndex != -1)
            {
                publishFamilyDataToForm(((List<FamilyEntity>)gridFamily.DataSource)[e.RowIndex]);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (currentFamilyMem != -1)
            {
                if (showConfirmDeleteMessage())
                {
                    if (Family.Delete(currentFamilyMem))
                    {
                        clearFamilyData();
                        refreshFamilyGrid();
                        showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                    }
                }

            }
            else
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            clearPartnerData();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else
            {
                PartnerEntity oldEnttiy = Partner.GetPartnerByMemId(currentMem);
                PartnerEntity newEntity = fillPartnerEntityFromForm();
                if (newEntity == null)
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                else
                {
                    if (oldEnttiy != null)
                    {
                        newEntity.Id = oldEnttiy.Id;
                        flag &= Partner.Update(newEntity);
                    }
                    else
                        flag &= Partner.Add(newEntity);

                    if (flag)
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else if (showConfirmDeleteMessage())
            {
                if (Partner.DeleteByMemId(currentMem))
                {
                    clearPartnerData();
                    showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                }
            }
        }

        private void chkPartnerExisting_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPartnerExisting.Checked)
            {
                SelectMemberDialoge dialoge = new SelectMemberDialoge(1);
                dialoge.mainForm = this;
                dialoge.ShowDialog(this);
            }
            else
            {
                lblExistingPartner.Text = "";
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            clearAddEduPageData();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
            else
            {
                EducationEntity entity = fillEducationEntityFromForm();
                bool flag = true;
                if (entity == null)
                {
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                }
                else
                {
                    if (entity.Id == -1)
                        flag &= Education.Add(entity) == -1 ? false : true;
                    else
                        flag &= Education.Update(entity);

                    if (flag)
                    {
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                        clearAddEduPageData();
                        refreshEduGrid();
                    }
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }

        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (currentAddEdu != -1)
            {
                if (showConfirmDeleteMessage())
                {
                    if (Education.Delete(currentAddEdu))
                    {
                        clearAddEduPageData();
                        refreshEduGrid();
                        showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                    }
                }

            }
            else
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
        }

        private void gridAddEdu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 || e.ColumnIndex != -1)
            {
                publishEducationDataToForm(((List<EducationEntity>)gridAddEdu.DataSource)[e.RowIndex]);
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            clearCoursesPageData();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (currentMem == -1)
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
            else
            {
                CoursesEntity entity = filCourseEntityFromForm();
                bool flag = true;
                if (entity == null)
                {
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                }
                else
                {
                    if (entity.C_Id == -1)
                        flag &= Courses.Add(entity);
                    else
                        flag &= Courses.Update(entity);

                    if (flag)
                    {
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                        clearCoursesPageData();
                        refreshCoursesGrid();
                    }
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }
        }

        private void gridCourses_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 || e.ColumnIndex != -1)
            {
                publishCourseEntityToForm(((List<CoursesEntity>)gridCourses.DataSource)[e.RowIndex]);
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (currentCourse != -1)
            {
                if (showConfirmDeleteMessage())
                {
                    if (Courses.Delete(currentCourse))
                    {
                        clearCoursesPageData();
                        refreshCoursesGrid();
                        showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                    }
                }

            }
            else
            {
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            clearJobPageData();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else
            {
                JobEntity oldEnttiy = MJob.GetJobByMemId(currentMem);
                JobEntity newEntity = fillJobEntityFromForm();
                if (newEntity == null)
                    showMessage(MESSAGE_DATA_NOT_COMPLETED);
                else
                {
                    if (oldEnttiy != null)
                    {
                        newEntity.Id = oldEnttiy.Id;
                        flag &= MJob.Update(newEntity);
                    }
                    else
                        flag &= MJob.Add(newEntity);

                    if (flag)
                        showMessage(MESSAGE_SAVED_SUCCESSFULLY);
                    else
                        showMessage(MESSAGE_ERROR_IN_SAVE);
                }
            }

        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            if (currentMem == -1)
                showMessage(MESSAGE_MEM_NOT_SELECTED);
            else if (showConfirmDeleteMessage())
            {
                if (MJob.Delete(currentMem))
                {
                    clearJobPageData();
                    showMessage(MESSAGE_DELETE_SUCCESSFULLY);
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            refreshCombos();
            setPhotosRequiredValues();
            FilltxtGroup();
            //  cmboMainJobType.Items.AddRange(MJob.getJobTypes().ToArray());
        }

        private void setPhotosRequiredValues()
        {
            if (Directory.Exists(picsPath + "\\temp"))
                Directory.Delete(picsPath + "\\temp", true);

            String applicationPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            picsPath = applicationPath.Remove(applicationPath.IndexOf("MasMembers.exe"));
            picsPath += "photos\\";
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            flag = 1;
            // Connect the Paint event of the PictureBox to the event handler method.
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.pictureBox1.Image = Image.FromFile(this.openFileDialog1.FileName);
                picExt = this.openFileDialog1.FileName.Split(new char[] { '.' })[1];
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            using (Font myFont = new Font("Arial", 10, FontStyle.Bold))
            {
                if (flag == 0 && this.pictureBox1.Image == null)
                {
                    e.Graphics.DrawString("أضغط لإضافة الصورة", myFont, Brushes.Red, new Point(0, 0));
                }
                else
                {
                    e.Graphics.DrawString("", myFont, Brushes.Transparent, new Point(0, 0));
                }
            }
        }

        private void txtMemName_TextChanged(object sender, EventArgs e)
        {

        }

        private void label22_Click(object sender, EventArgs e)
        {

        }



    }

}


